package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import pojo.User;
import service.UserManager;
import service.impl.UserManagerFactory;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserManager userManager;
       
    public LoginServlet() {
        super();
        userManager = UserManagerFactory.getInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		
		User u = userManager.getUserByName(uname);
		if(u!=null && pwd.equals(u.getPwd())){
			Logger logger = Logger.getLogger(LoginServlet.class);
			logger.info(uname+" login!");
			request.getSession().setAttribute("user", u);
			request.getRequestDispatcher("sec/home").forward(request, response);
		}else{
			response.sendRedirect("login.jsp");
		}
	}

}
